package me.a01mymap;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class A05_MapDemo5 {
    public static void main(String[] args) {
        //HashMap的键位置如果存储的是自定义对象，需要重写hashCode和equals方法
        HashMap<Student, String> hm = new HashMap<>();

        Student s1 = new Student("zhangsan", 23);
        Student s2 = new Student("lisi", 24);
        Student s3 = new Student("wangwu", 25);
        Student s4 = new Student("lisi", 24);

        hm.put(s1, "浙江");
        hm.put(s2, "江苏");
        hm.put(s3, "河南");
        hm.put(s4, "河北");

        //遍历集合
        Set<Student> keys = hm.keySet();
        for (Student key : keys) {
            String value = hm.get(key);
            System.out.println(key + " = " + value);
        }
        System.out.println("------------------------");

        //第二种遍历
        Set<Map.Entry<Student, String>> entries = hm.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + " = " + value);
        }
        System.out.println("------------------------");

        //lambda表达式
        hm.forEach((student, s) -> {
            System.out.println(student + " = " + s);
        });
    }
}
